package copyengine.ui.component.tree.dataprovider
{
	import copyengine.utils.GeneralUtils;

	public final class CETreeDataProvider implements ICETreeDataProvider
	{
		private var dataProvider:Vector.<ICETreeCellRenderData>;

		public function CETreeDataProvider()
		{
			dataProvider = new Vector.<ICETreeCellRenderData>();
		}

		public function clear():void
		{
			dataProvider.length = 0;
		}

		public function setDataVector(_data:Vector.<ICETreeCellRenderData>):void
		{
			clear();
			if (_data)
			{
				for (var i:int = 0; i < _data.length; i++)
				{
					dataProvider.push(_data[i]);
				}
			}
		}

		public function getData():Vector.<ICETreeCellRenderData>
		{
			return dataProvider;
		}

		public function pushData(_data:ICETreeCellRenderData):void
		{
			dataProvider.push(_data);
		}

		public function deleteData(_data:ICETreeCellRenderData):void
		{
			GeneralUtils.removeElementFromArrayOrVector(_data, dataProvider);
		}

		public function sort(_sortFunction:Function):void
		{
			dataProvider.sort(_sortFunction);
		}

		public function forEach(_forEachFunction:Function):void
		{
			dataProvider.forEach(_forEachFunction);
		}

		public function filter(_filterFunction:Function):Vector.<ICETreeCellRenderData>
		{
			return dataProvider.filter(_filterFunction);
		}

		public function dispose():void
		{
			dataProvider = null;
		}

		public function getGroupIndexByData(_data:ICETreeCellRenderData):int
		{
			for (var i:int = 0; i < dataProvider.length; i++)
			{
				if (dataProvider[i] == _data)
				{
					return i;
				}
			}
			return int.MIN_VALUE;
		}
	}
}
